//
//  HomeHotController.swift
//  KkmhSwift
//
//  Created by tenltrs on 2018/11/9.
//  Copyright © 2018 Tenltrs. All rights reserved.
//

import UIKit

class HomeHotController: BaseChildViewController,SGPageTitleViewDelegate,SGPageContentScrollViewDelegate,HomeSubHotControllerDelegate {
    
    
    var titleArr : [HomeTimeModel] = []
    
    var titleView : SGPageTitleView!
    
//    var tableView : UITableView!
    var contentView : SGPageContentScrollView!
    
    var isDragging = false
    
    var vcArray : [HomeSubHotController] = []
    
    var scrollView : UIScrollView?

    override func viewDidLoad() {
        super.viewDidLoad()

        let backView = UIViewTool.mas_view(backColor: UIColor.NavColor)
        backView.frame=CGRect(x: 0, y: 0, width: ScreenWidth, height: SafeAreaInsets.top)
        view.addSubview(backView)
        
        initTitleData()
        
        initTitleView()
        
        initContentView()
        
        titleView.selectedIndex=titleArr.count-1
        
        view.layoutIfNeeded()

    }

    
    // MARK: -- 创建顶部title数据
    func initTitleData() {
        
        let timeArr : [Date] = CommUtls.latelyTime(ofDay: 7) as! [Date]
        //
        for (index,item) in timeArr.enumerated() {
            
            let model = HomeTimeModel()
            model.time=NSInteger((CommUtls.zero(of: item)!.timeIntervalSince1970))
            if(index==timeArr.count-1){
                model.week="今天"
            }else if(index==timeArr.count-2){
                model.week="昨天"
            }else{
                
                model.week=CommUtls.week(withDate: model.time! as NSNumber)
            }
            
            self.titleArr.append(model)
        }
        
    }
    
    
    func initTitleView(){
        
        var arr = [String]()
        for item in titleArr {
            arr.append(item.week!)
        }
        
        let str : NSString = titleArr.first!.week! as NSString
        let size = str.boundingRect(with: CGSize(width: 999.0, height: 999.0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: [NSAttributedString.Key.font:UIFont.systemFont(ofSize: 13)], context: nil).size
        
        let config = SGPageTitleViewConfigure()
        config.bottomSeparatorColor = UIColor.LineColor
        config.titleFont = UIFont.boldSystemFont(ofSize: 13)
        config.titleAdditionalWidth = ScreenWidth/6-size.width
        config.titleSelectedFont = UIFont.boldSystemFont(ofSize: 14)
        config.titleColor = UIColor.BLACK6TEXTCOLOR
        config.titleSelectedColor = UIColor.FocusColor
        config.indicatorColor = UIColor.NavColor
        config.indicatorHeight=3.0
        config.indicatorStyle = SGIndicatorStyleFixed
        config.indicatorFixedWidth = 50
        
        titleView = SGPageTitleView.init(frame: CGRect(x: 0, y: NAVIGATIONBAR_HEIGHT, width: ScreenWidth, height: 35), delegate: self, titleNames: arr, configure: config)
        titleView.backgroundColor=UIColor.white
        self.view.addSubview(titleView!)
        
    }
    
    func initContentView(){
        
        for item in titleArr {
            
            let vc = HomeSubHotController.init(with: item.time!)
            vc.delegate=self
            vcArray.append(vc)
        }
        
        contentView=SGPageContentScrollView.init(frame: CGRect(x: 0, y: titleView.frame.maxY, width: ScreenWidth, height: ScreenHeight), parentVC: self, childVCs: vcArray)
        contentView.delegatePageContentScrollView=self
        contentView.isAnimated=true
        view.addSubview(contentView)
        
        for item in self.contentView.subviews {
            if item is UIScrollView {
                self.scrollView=item as? UIScrollView
                break;
            }
        }
    }
    
    func pageTitleView(_ pageTitleView: SGPageTitleView!, selectedIndex: Int) {
        
        contentView.setPageContentScrollViewCurrentIndex(selectedIndex)
    }
    
    func pageContentScrollView(_ pageContentScrollView: SGPageContentScrollView!, progress: CGFloat, originalIndex: Int, targetIndex: Int) {
        
        titleView.setPageTitleViewWithProgress(progress, originalIndex: originalIndex, targetIndex: targetIndex)

    }
    
    func pageContentScrollView(_ pageContentScrollView: SGPageContentScrollView!, index: Int) {
        if index-1>=0{
            let vc=self.vcArray[index-1];
            if (!self.children.contains(vc)) {
                vc.view.frame=CGRect(x: CGFloat(index-1) * self.scrollView!.width, y: 0, width: self.scrollView!.width, height: self.scrollView!.height);
                self.ts_addChild(vc, toView: self.scrollView!)
            }
        }
        
        if index+1 < self.vcArray.count{
            let vc=self.vcArray[index+1];
            if (!self.children.contains(vc)) {
                vc.view.frame=CGRect(x: CGFloat(index+1) * self.scrollView!.width, y: 0, width: self.scrollView!.width, height: self.scrollView!.height);
                self.ts_addChild(vc, toView: self.scrollView!)
            }
        }
    }
    
    func tableSlidingWithNavigation(hidden: Bool) {
        self.navigationController?.setNavigationBarHidden(hidden, animated: true)
        if iPhoneX{
            if hidden {
                UIView.animate(withDuration: 0.2) {
                    self.titleView.origin.y = SafeAreaInsets.top
                    self.contentView.origin.y = self.titleView.frame.maxY
                }
            }else{
                UIView.animate(withDuration: 0.2) {
                    self.titleView.origin.y = NAVIGATIONBAR_HEIGHT
                    self.contentView.origin.y = self.titleView.frame.maxY
                }
            }
        }else{
            if hidden {
                UIView.animate(withDuration: 0.2) {
                    self.titleView.origin.y = 0
                    self.contentView.origin.y = self.titleView.frame.maxY
                }
            }else{
                UIView.animate(withDuration: 0.2) {
                    self.titleView.origin.y = NAVIGATIONBAR_HEIGHT
                    self.contentView.origin.y = self.titleView.frame.maxY
                }
            }
            CURRENT_VC()?.statusBarHidden=hidden
        }
    }
    
    func bottomClickForwardAction(_ vc: HomeSubHotController?) {
        if vc == nil{
            return
        }
        let index = self.vcArray.index(of: vc!)
        if index! > 0{
            self.titleView.selectedIndex=index!-1
        }
    }
}
